﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VisualAgile.IBLL;
using VisualAgile.Model;
using System.Collections;

namespace VisualAgile.Web.Tests.Fakes
{
    public class FakeFeatureManager : IFeatureManager   
    {
        private List<Feature> featureList;

        public FakeFeatureManager(List<Feature> features)
        {
            featureList = features;
        }

        /// <summary>
        /// Create a feature (back to contain the default value)
        /// </summary>
        /// <returns></returns>
        public Feature NewFeature()
        {
            var feature = new Feature();

            feature.CreateTime = DateTime.Now;
            feature.Name = "New Feature";
            feature.Target = DateTime.Now.AddDays(27);
            feature.FullSize = true;               

            return feature;
        }

        /// <summary>
        /// Save feature
        /// </summary>
        /// <param name="feature">Feature entity</param>
        /// <returns></returns>
        public Feature Save(Feature feature)
        {
            Validate(feature);
            if (Get(feature.FeatureID) != null)
                throw new Exception("Existing feature");
            feature.FeatureID = Guid.NewGuid();            
            featureList.Add(feature);

            return Get(feature.FeatureID);
        }

        /// <summary>
        /// Access to features
        /// </summary>
        /// <param name="featureId">Feature ID</param>
        /// <returns></returns>
        public Feature Get(Guid featureId)
        {
            return featureList.SingleOrDefault(u => u.FeatureID == featureId);
        }

        /// <summary>
        /// Access to features
        /// </summary>
        /// <param name="featureId">Feature ID</param>
        /// <returns></returns>
        public IList GetByProject(Guid projectId)
        {
            return new List<Feature>();
        }

        /// <summary>
        /// Access to features
        /// </summary>
        /// <param name="featureId">Feature ID</param>
        /// <returns></returns>
        public IList GetByRelease(string releaseId)
        {
            return featureList.FindAll(f => f.ReleaseID == releaseId);
        }      

        /// <summary>
        /// Delete Feature
        /// </summary>
        /// <param name="featureId">Feature ID</param>
        /// <returns></returns>
        public void Delete(Guid featureId)
        {
            var feature = Get(featureId);
            if (feature == null) throw new Exception("NotFound");
            featureList.Remove(feature);
        }

        /// <summary>
        /// Update Feature
        /// </summary>
        /// <param name="feature">Feature Entity</param>
        /// <returns></returns>
        public Feature Update(Feature feature)
        {
            Validate(feature);
            Feature oldFeature = Get(feature.FeatureID);
            if (oldFeature != null)            
                featureList.Remove(oldFeature);                            
            else            
                feature.FeatureID = Guid.NewGuid();                           

            featureList.Add(feature);
            return Get(feature.FeatureID);
        }

        /// <summary>
        /// Access to features
        /// </summary>
        /// <returns></returns>
        public IList GetAll()
        {
            return featureList;
        }

        private void Validate(Feature feature)
        {
            //TODO throw exception on error.
        }        
    }
}
